package com.gwnet.shop.service.impl;

import java.util.List;
import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.domain.OrderEvaluateAlbum;
import com.gwnet.shop.domain.OrderInfo;
import com.gwnet.shop.mapper.OrderEvaluateAlbumMapper;
import com.gwnet.shop.mapper.OrderInfoMapper;
import org.springframework.stereotype.Service;
import com.gwnet.shop.mapper.OrderEvaluateMapper;
import com.gwnet.shop.domain.OrderEvaluate;
import com.gwnet.shop.service.IOrderEvaluateService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/**
 * 商品评价Service业务层处理
 * @author zhangwanpeng
 * @date 2024-08-07
 */
@Service
public class OrderEvaluateServiceImpl implements IOrderEvaluateService 
{
    @Resource
    private OrderEvaluateMapper orderEvaluateMapper;

    @Resource
    private OrderEvaluateAlbumMapper orderEvaluateAlbumMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;


    /**
     * 查询商品评价
     * 
     * @param id 商品评价主键
     * @return 商品评价
     */
    @Override
    public OrderEvaluate selectOrderEvaluateById(Long id)
    {
        return orderEvaluateMapper.selectOrderEvaluateById(id);
    }

    /**
     * 查询商品评价列表
     * 
     * @param orderEvaluate 商品评价
     * @return 商品评价
     */
    @Override
    public List<OrderEvaluate> selectOrderEvaluateList(OrderEvaluate orderEvaluate)
    {
        List<OrderEvaluate> list = orderEvaluateMapper.selectOrderEvaluateList(orderEvaluate);
        for(OrderEvaluate evaluate:list){
            //查询每条记录上传的图片
            OrderEvaluateAlbum album = new OrderEvaluateAlbum();
            album.setEvaluateId(evaluate.getId());
            List<OrderEvaluateAlbum> albums = orderEvaluateAlbumMapper.selectOrderEvaluateAlbumList(album);
            evaluate.setAlbums(albums);
        }
        return list;
    }

    /**
     * 新增商品评价
     * 
     * @param orderEvaluate 商品评价
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrderEvaluate(OrderEvaluate orderEvaluate)
    {
        //TODO 获取用户信息
        //LoginUser loginUser = LoginHelper.getLoginUser();
        //Long userId = loginUser.getUserId();
        //String nickname = loginUser.getNickname();
        OrderInfo orderInfo = orderInfoMapper.selectById(orderEvaluate.getOrderId());
        Long userId = 6L;

        //TODO 判断是否是自己的订单
        if(orderInfo.getUserId().longValue() != userId.longValue()){
            throw new RuntimeException("您没有评价该订单的权限!");
        }

        orderEvaluate.setUserId(userId);
        orderEvaluate.setNickName("用户昵称");
        orderEvaluate.setAvatar("头像地址");
        orderEvaluate.setCreateTime(DateUtils.getNowDate());

        List<OrderEvaluateAlbum> albums = orderEvaluate.getAlbums();
        int rows = orderEvaluateMapper.insertOrderEvaluate(orderEvaluate);
        if(albums != null && albums.size() > 0){
            for(OrderEvaluateAlbum album:albums){
                album.setEvaluateId(orderEvaluate.getId());
                orderEvaluateAlbumMapper.insertOrderEvaluateAlbum(album);
            }
        }
        return rows;
    }

    /**
     * 修改商品评价
     * 
     * @param orderEvaluate 商品评价
     * @return 结果
     */
    @Override
    public int updateOrderEvaluate(OrderEvaluate orderEvaluate)
    {
        return orderEvaluateMapper.updateOrderEvaluate(orderEvaluate);
    }

    /**
     * 批量删除商品评价
     * 
     * @param ids 需要删除的商品评价主键
     * @return 结果
     */
    @Override
    public int deleteOrderEvaluateByIds(Long[] ids)
    {
        return orderEvaluateMapper.deleteOrderEvaluateByIds(ids);
    }

    /**
     * 删除商品评价信息
     * @param id 商品评价主键
     * @return 结果
     */
    @Override
    public int deleteOrderEvaluateById(Long id)
    {
        return orderEvaluateMapper.deleteOrderEvaluateById(id);
    }

    /**
     * 根据店铺id 查询商品评价列表
     * @param tenantId
     * @return
     */
    @Override
    public List<OrderEvaluate> selectOrderEvaluateListByTenantId(Long tenantId) {
        List<OrderEvaluate> list = orderEvaluateMapper.selectOrderEvaluateListByTenantId(tenantId);
        for(OrderEvaluate evaluate:list){
            //查询每条记录上传的图片
            OrderEvaluateAlbum album = new OrderEvaluateAlbum();
            album.setEvaluateId(evaluate.getId());
            List<OrderEvaluateAlbum> albums = orderEvaluateAlbumMapper.selectOrderEvaluateAlbumList(album);
            evaluate.setAlbums(albums);
        }
        return list;
    }
}
